﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Core.Components.Responser;

namespace RiderShop.Mobile.Modle.MyAccount
{
    public class MyFavoritesModel
    {
        /// <summary>
        /// 用户的收藏列表集合
        /// </summary>
        public List<ProductBasicInfoExtend> FavoritesList { get; set; }

        /// <summary>
        /// 得到用户的收藏列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页得到多少条</param>
        /// <returns></returns>
        public DbQueryResult<List<ProductBasicInfoExtend>> GetUserFavorites(int pageIndex, int pageSize)
        {
            string url = SiteUrl.FormatUrl("ProductManagement_GetProductFavoriteList");
            var query = new ProductQuery()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                InUser = WebClientContext.Current.UserId
            };
            DbQueryResult<List<ProductBasicInfoExtend>> result = RestfulHelper.Query<ProductBasicInfoExtend>(url, query);
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = result.TotalRecords;
            TotalPage = (int)Math.Ceiling(TotalCount / (double)pageSize);

            return result;
        }

        /// <summary>
        /// 检查这个商品是否已经被收藏过
        /// </summary>
        /// <param name="productId">产品编号</param>
        /// <returns>是否已收藏</returns>
        public ServiceResponser CheckProductAlreadyFavorite(string productId)
        {
            string url = SiteUrl.FormatUrl("ProductManagement_CheckAlreadyFavorite", WebClientContext.Current.UserId, productId);

            var result = RestfulHelper.QueryWithSingle<ServiceResponser>(url);

            return result;
        }

        //是否有上一页
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }

        //是否有下一页
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPage); }
        }

        //页面索引值
        private int _pageIndex;
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        //每页记录的数量
        private int _pageSize;
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }


        //总记录数
        private int _totalCount;
        public int TotalCount
        {
            get { return _totalCount; }
            set { _totalCount = value; }
        }

        //总页数
        private int _totalPage;
        public int TotalPage
        {
            get { return _totalPage; }
            set { _totalPage = value; }
        }
    }
}
